<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>维修申请</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">

		<link rel="stylesheet" type="text/css" href="css/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="css/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="js/demo.css">
		<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
		<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
		<link rel="stylesheet" type="text/css" href="css/usedcar.css">
   		<script type="text/javascript">  
		$(document).ready(function(){
 		$('#dg').datagrid({ 
			remoteSort : false,
			pageSize: 10,  	
			pageList: [10, 15, 20, 30, 50],
			loadFilter: pagerFilter,
			columns:[[  
			          {field:'oddId',title:'预约单号',width:80,align:'center', sortable:'true'},  
			          {field:'createDate',title:'创建日期',width:80,align:'center', sortable:'true',
			        	  formatter:function(value,row){
								var str = row.createDate;
								var m = str.month+1;
								var d = str.date;
								return (str.year+1900)+"-"+(m<10?('0'+m):m)+"-"+(d<10?('0'+d):d);
			        	  }},  
			          {field:'oddDate',title:'预约日期',width:80,align:'center', sortable:'true',
			        	  formatter:function(value,row){
								var str = row.oddDate;
								var m = str.month+1;
								var d = str.date;
								return (str.year+1900)+"-"+(m<10?('0'+m):m)+"-"+(d<10?('0'+d):d);
			        	  }},  
			          {field:'oddCompany',title:'预约公司',width:200,align:'left', sortable:'true',
			        	  formatter:function(value,row){
								var companys = ${companys};
								for(var i=0;i<companys.length;i++){
									if(row.oddCompany == companys[i].id){
										return companys[i].name;
									}
								}
			        	  }},  
			          {field:'vin',title:'车辆识别号',width:80,align:'left', sortable:'true'}, 
			          {field:'status',title:'状态',width:30,align:'left', sortable:'true',
			        	  formatter:function(value,row){
								var str = row.status;
								if(str == '1'){
									return '新';
								}else if(str == '2'){
									return '通过';
								}else if(str == '3'){
									return '未通过';
								}
								return (str.year+1900)+"-"+(m<10?('0'+m):m)+"-"+(d<10?('0'+d):d);
			        	  }} 
			]],
 			loader: function(param, success, error) {
	 			success(${odds});
	 		}
 		}); 
	 		var d = ${carMain};
			$('#vin').empty();
			for ( var i = 0; i < d.length; i++) {
				$('#vin').append("<option>"+d[i].vin+"</option>");
			} 
		})
 		function pagerFilter(data){
			if ($.isArray(data)){	// is array
					data = {
						total: data.length,
						rows: data
					}
			}
			var dg = $('#dg');
			var opts = dg.datagrid('options');
			var pager = dg.datagrid('getPager');
			pager.pagination({
				onSelectPage:function(pageNum, pageSize){
					opts.pageNumber = pageNum;
					opts.pageSize = pageSize;
					pager.pagination('refresh',{
						pageNumber:pageNum,
						pageSize:pageSize
					});
					dg.datagrid('loadData',data);
				},
				onRefresh:function(pageNumber, pageSize){
					dg.datagrid('reload');
				}
			});
			if (!data.originalRows){
				data.originalRows = (data.rows);
			}
			var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
			var end = start + parseInt(opts.pageSize);
			data.rows = (data.originalRows.slice(start, end));
			return data;
		}
    
        //var url;  
        function newUser(){  
            $('#dlg').dialog('open').dialog('setTitle','新建申请单');  
            $('#fm').form('clear');
	        document.getElementById('fm').action="/aasimp/mok/maintenance/odd_addOdd";  
             
			var date = new Date();
			var y = date.getFullYear();  
			var m = date.getMonth()+1;  
			var d = date.getDate();
			var h = date.getHours();
			var f = date.getMinutes();
			var s = date.getSeconds();
			$('#createDate').val(y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d));
			$('#oddId').val(y+(m<10?('0'+m):m)+(d<10?('0'+d):d)+(h<10?('0'+h):h)+(f<10?('0'+f):f)+(s<10?('0'+s):s)); 
			$('#persontel').numberbox('setValue', ${eauser.tel});
            $('#personemail').val("${eauser.email}");
        }  
        function editUser(){  
            var row = $('#dg').datagrid('getSelected');  
            if (row){
				var date = new Date();
            	if(row.status != '1'){
            		alert("已审批不能修改！");
            		return;
            	}
            	if(row.oddDate.time < date.getTime()){
            		alert("已过期不能修改！");
            		return;
            	}
                $('#dlg').dialog('open').dialog('setTitle','修改申请单');  
                $('#oddId').val(row.oddId);
                $('#createDate').val(formatDate(row.createDate));
                $('#oddDate').datebox('setValue', formatDate(row.oddDate));
                $('#oddCompany').combogrid('setValue', row.oddCompany);
                $('#persontel').numberbox('setValue', row.persontel);
                $('#personemail').val(row.personemail);
                
				document.getElementById('vin').value = row.vin;
                $('#remark').val(row.remark);
	        	document.getElementById('fm').action='/aasimp/mok/maintenance/odd_addOdd?id='+row.oddId; 
	        	
            }  
        }
        function formatDate(str){
			var m = str.month+1;
			var d = str.date;
        	return (str.year+1900)+"-"+(m<10?('0'+m):m)+"-"+(d<10?('0'+d):d);
        }  
        function saveUser(){
        	if($('#fm').form('validate')){
	            $('#fm').submit();
	            $('#dlg').dialog('close');
        	}   
             
           /* $('#fm').form('submit',{  
                url: url,  
                onSubmit: function(){  
                    return $(this).form('validate');  
                },  
                success: function(result){  
                    var result = eval('('+result+')');
                    if (result.errorMsg){  
                        $.messager.show({  
                            title: 'Error',  
                            msg: result.errorMsg  
                        });  
                    } else {
                        $('#dlg').dialog('close');      // close the dialog  
                        $('#dg').datagrid('reload');    // reload the user data  
                    }  
                }  
            });*/    
        }  
        function destroyUser(){  
            var row = $('#dg').datagrid('getSelected');  
            if (row){  
                $.messager.confirm('提示','确定删除这条记录吗?',function(r){  
                    if (r){
                      	window.location = '/aasimp/mok/maintenance/odd_deleteodd?oddId='+row.oddId;
                        /*$.post('cosmetology/carCosmetologyAction_destroyApply',{id:row.oddId},function(result){  
                            if (result.success){  
                                $('#dg').datagrid('reload');    // reload the user data  
                            } else {  
                                $.messager.show({   // show error message  
                                    title: 'Error',  
                                    msg: result.errorMsg  
                                });  
                            }  
                        },'json'); */ 
                    }  
                });  
            }  
        }  
		</script>
		<style type="text/css">
		#main {
			width: 960px;
		} 
        #fm{  
            margin:0;  
            padding:10px 30px;  
        }  
        .ftitle{  
            font-size:14px;  
            font-weight:bold;  
            padding:5px 0;  
            margin-bottom:10px;  
            border-bottom:1px solid #ccc;  
        }  
        .fitem{  
            margin-bottom:5px;  
        }  
        .fitem label{  
            display:inline-block;  
            width:80px;  
        }
    </style>

	</head>

<body>
<div id="main">
	<table id="dg" title="我的维修申请单" class="easyui-datagrid" style="width:960px;height:364px"  
            toolbar="#toolbar" pagination="true"
            rownumbers="true" fitColumns="true" singleSelect="true">   
    </table>  
    <div id="toolbar">  
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新建</a>  
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑</a>  
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除</a>  
    </div>  
      
    <div id="dlg" class="easyui-dialog" style="width:400px;height:350px;padding:10px 20px"  
            closed="true" buttons="#dlg-buttons">  
        <form id="fm" method="post">  
            <table>  
                <tr>  
                    <td>预约单号:</td>  
                    <td><input class="easyui-validatebox" type="text" id="oddId" name="oddId" readonly="readonly"></input></td>  
                </tr>  
                <tr>  
                    <td>创建日期:</td>  
                    <td><input class="easyui-validatebox" type="text" id="createDate" name="createDate" readonly="readonly"></input></td>  
                </tr>  
                <tr>  
                    <td>预约日期:</td>  
                    <td><input class="easyui-datebox" type="text" id="oddDate" name="oddDate" data-options="required:true,missingMessage:'此项必填',formatter:myformatter,parser:myparser,validType:'md[\'1991-01-02\']'"></input>
                    <script type="text/javascript">  
				        function myformatter(date){  
				            var y = date.getFullYear();  
				            var m = date.getMonth()+1;  
				            var d = date.getDate();  
				            return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);  
				        }  
				        function myparser(s){  
				            if (!s) return new Date();  
				            var ss = (s.split('-'));  
				            var y = parseInt(ss[0],10);  
				            var m = parseInt(ss[1],10);  
				            var d = parseInt(ss[2],10);  
				            if (!isNaN(y) && !isNaN(m) && !isNaN(d)){  
				                return new Date(y,m-1,d);  
				            } else {  
				                return new Date();  
				            }  
				        }  
				        $.extend($.fn.validatebox.defaults.rules, {  
				            md: {  
				                validator: function(value, param){ 
									var date = new Date();  
				                    var d2 = $.fn.datebox.defaults.parser(value);  
				                    return d2>=date;  
				                },  
				                message: '预约时间必须为今天之后'  
				            }
				              
				        })  
				    </script>
                    </td>  
                </tr>  
                <tr>  
                    <td>预约公司:</td>  
                    <td><select class="easyui-combogrid" id="oddCompany" name="oddCompany" style="width:134px" data-options="  
                    			required:true,
                    			missingMessage:'此项必填',
					            panelWidth: 500,  
					            idField: 'id',  
					            textField: 'name',    
					            columns: [[  
					                {field:'id',title:'公司编号',hidden:true},  
					                {field:'name',title:'公司名称',width:160,align:'left'},  
					                {field:'address',title:'公司地址',width:160,align:'left'},  
					                {field:'username',title:'负责人',width:50,align:'left'},  
					                {field:'tel',title:'电话',width:75,align:'left'}  
					            ]],
					            loader:function(param, success, error) {
					            	success(${companys});
					            },  
					            fitColumns: true  
					        ">  
					    </select> 
				    </td>  
                </tr>  
                <tr>  
                    <td>手机:</td>  
                    <td><input class="easyui-numberbox" id="persontel" name="persontel" data-options="
                    		required:true,
                    		missingMessage:'此项必填',
                    		validType:'maxLength[11]',  
				            onChange: function(value){  
				                $('#persontel').text(value);  
				            }">
				            <script type="text/javascript">
				            $.extend($.fn.validatebox.defaults.rules, {  
							    maxLength: {  
							        validator: function(value, param){  
							            return value.length <= param[0];  
							        },  
							        message: '最多输入{0}位'  
							    }  
							});
				            </script>
		            </td>  
                </tr>  
                <tr>  
                    <td>邮箱:</td>  
                    <td><input class="easyui-validatebox" type="text" id="personemail" name="personemail" data-options="required:true,missingMessage:'此项必填',validType:'email'"></input></td>  
                </tr>  
                <tr>  
                    <td>车辆识别号:</td>  
                    <td>  
                        <select id="vin" name="vin"></select>  
                    </td>  
                </tr>  
                <tr>  
                    <td>维修原因:</td>  
                    <td><textarea id="remark" name="remark" style="height:60px;"></textarea></td>  
                </tr>  
            </table>  
        </form>  
    </div>  
    <div id="dlg-buttons">  
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a>  
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>  
    </div>  
</div>
</body>
</html>